/**
 * @author tsdy
 * @description 组件资源中心管理器
 */

import * as PIXI from 'pixi.js';
import Resource from './Resource';
import { ResourceCenterConfig } from '../typings/resource';
class ResourceCenter extends Resource {
    $sprite: Record<string, PIXI.Texture | Array<PIXI.Texture>> = {};
    $animatedSprite: Record<string, Array<PIXI.Texture>> = {};
    _config: ResourceCenterConfig;
    // 这个map中保存了资源加载成功后的回调函数
    _map: Map<string, Array<Function>> = new Map();
    constructor(config: ResourceCenterConfig) {
        super(config);
        this._config = config;
    }
    public async init() {
        await this.initResource();
        this.initSprite();
        this.initAnimatedSprite();
    }
    public addLoadListener(name: string, fun: Function) {
        if (!this._map.has(name)) {
            this._map.set(name, []);
        }
        (<Array<Function>>this._map.get(name)).push(fun);
    }
    public removeLoadlistener(name: string, fun: Function) {
        if (this._map.has(name)) {
            this._map.set(name, this._map.get(name)!.filter(item => {
                if (item == fun) {
                    return false
                } else {
                    return true;
                }
            }))
        }
    }
    public getTexture(name: string): PIXI.Texture {
        return this.texture[name];
    }
    public getTextures(name: string): Array<PIXI.Texture> {
        return this.textures[name];
    }
    public getAudio(name: string): HTMLAudioElement {
        return this.audio[name];
    }
    private initSprite() {
        const { sprite } = this._config;
        if (sprite) {
            Object.keys(sprite).forEach(key => {
                this.$sprite[key] = this.getTexture(sprite[key])? this.getTexture(sprite[key]): this.getTextures(sprite[key]);
                // 根据资源名称调用使用addLoadListener添加的资源监听器函数
                let callbackArr = this._map.get(key);
                if (callbackArr) {
                    callbackArr.forEach(ca => {
                        ca();
                    })
                }
                this._map.delete(key);
            })
        }
    }
    private initAnimatedSprite() {
        const { animatedSprite } = this._config;
        if (animatedSprite) {
            Object.keys(animatedSprite).forEach(key => {
                const { name, range } = animatedSprite[key];
                let textureArr: Array<PIXI.Texture>;
                textureArr = this.getTextures(name)
                if (range) {
                    const { min, max } = range;
                    textureArr = textureArr.slice(min, max);
                }
                this.$animatedSprite[key] = textureArr;
                
                // 根据资源名称调用使用addLoadListener添加的资源监听器函数
                let callbackArr = this._map.get(key);
                if (callbackArr) {
                    callbackArr.forEach(ca => {
                        ca();
                    })
                }
                this._map.delete(key);
            })
        }
    }
}

export default ResourceCenter;